package com.dgt.trade.controller;

import com.dgt.trade.entitys.yifu.Subaccounts_login;
import com.dgt.trade.entitys.yifu.Subaccounts_select;
import com.dgt.trade.utils.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dgt.trade.entitys.User;
import com.dgt.trade.service.IUserService;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class LoginController extends BaseController {
    @Autowired
    private IUserService userService;

    Integer status = null;
    String message = null;
    String SubAccountID = null;
    String SubAccountName = null;
    String AgentName = null;
    String Link = null;
    String SubLink = null;
    String time = String.valueOf(new Date().getTime());

    @RequestMapping("/login")
    @ResponseBody
    public Data login() {
        Data data = new Data();
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        if (StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(password)) {

            //逸富查询子账户
            Map<String, String> select_map = new LinkedHashMap<String, String>();
            Subaccounts_select subaccounts_select = new Subaccounts_select();
            subaccounts_select.setParams(mobile);
            subaccounts_select.setType("1");
            subaccounts_select.setTimeStamp(time);
            subaccounts_select.setVersion("1.0");
            String MD5 = MD5Util.MD5(subaccounts_select.getParams(), subaccounts_select.getType(), subaccounts_select.getTimeStamp(), subaccounts_select.getVersion());
            subaccounts_select.setSignMsg(MD5);

            select_map.put("Params", subaccounts_select.getParams());
            select_map.put("Type", subaccounts_select.getType());
            select_map.put("TimeStamp", subaccounts_select.getTimeStamp());
            select_map.put("Version", subaccounts_select.getVersion());
            select_map.put("SignMsg", subaccounts_select.getSignMsg());

            String query = MapToStringSplicing.createLinkStringByGet(select_map);

            try {
                String result =  HttpClientUtil.getProcessDefinitionList("http://120.55.162.84:8050/api/subaccounts?"+query,select_map);
                JsonParser parser = new JsonParser();
                JsonObject object = (JsonObject) parser.parse(result);
                JsonArray array = null;
                JsonObject subObject = null;
                status = object.get("status").getAsInt();
                if (status == 0) {
                    array = object.get("data").getAsJsonArray();
                    for (int i = 0; i < array.size(); i++) {
                        subObject = array.get(i).getAsJsonObject();
                        SubAccountID = subObject.get("SubAccountID").getAsString();
                        SubAccountName = subObject.get("SubAccountName").getAsString();
                        AgentName = subObject.get("AgentName").getAsString();
                        Link = subObject.get("Link").getAsString();
                        SubLink = subObject.get("SubLink").getAsString();
                    }
                } else {
                    message = object.get("message").getAsString();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            User user = userService.getMemberByMobile(mobile);

            if (user != null && MD5Util.MD5(mobile, password).equals(user.getPassword())) {

                //逸富登录
                Map<String, String> login_map = new LinkedHashMap<String, String>();
                Subaccounts_login login = new Subaccounts_login();
                login.setPassword(password);
                login.setSubAccountID(SubAccountID);
                login.setTimeStamp(time);
                login.setVersion("1.0");
                String str = MD5Util.MD5_login(login.getPassword(), login.getSubAccountID(), login.getTimeStamp(), login.getVersion());
                login.setSignMsg(str);

                login_map.put("Password", login.getPassword());
                login_map.put("SubAccountID", login.getSubAccountID());
                login_map.put("TimeStamp", login.getTimeStamp());
                login_map.put("Version", login.getVersion());
                login_map.put("SignMsg", login.getSignMsg());

                String query1 = MapToStringSplicing.createLinkStringByGet(login_map);

                String result = null;
                try {
                    result =  HttpClientUtil.postHistoryProcessInstancesList("http://120.55.162.84:8050/api/subaccounts/login?"+query,select_map);
                    JsonParser parser = new JsonParser();
                    JsonObject object = (JsonObject) parser.parse(result);
                    status = object.get("status").getAsInt();
                    message = object.get("message").getAsString();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if(status == 0) {
                    user.setPassword(null);
                    request.getSession().setAttribute(ConstantsUtils.LOGIN_KEY_FLAG, JSON.toJSONString(user));
                    data.setCode(200);
                    data.setMsg("登录成功");
                    return data;
                }else{
                    data.setCode(status);
                    data.setMsg(message);
                    return data;
                }
            } else {
                data.setCode(4001);
                data.setMsg(" 用户名或密码错误");
                return data;
            }
        } else {
            data.setCode(4002);
            return data;
        }
    }

    @RequestMapping("/loginIn")
    @ResponseBody
    public Data loginIn() {
        Data data = new Data();
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        if (StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(password)) {

            //逸富查询子账户
            Map<String, String> select_map = new LinkedHashMap<String, String>();
            Subaccounts_select subaccounts_select = new Subaccounts_select();
            subaccounts_select.setParams(mobile);
            subaccounts_select.setType("1");
            subaccounts_select.setTimeStamp(time);
            subaccounts_select.setVersion("1.0");
            String MD5 = MD5Util.MD5(subaccounts_select.getParams(), subaccounts_select.getType(), subaccounts_select.getTimeStamp(), subaccounts_select.getVersion());
            subaccounts_select.setSignMsg(MD5);

            select_map.put("Params", subaccounts_select.getParams());
            select_map.put("Type", subaccounts_select.getType());
            select_map.put("TimeStamp", subaccounts_select.getTimeStamp());
            select_map.put("Version", subaccounts_select.getVersion());
            select_map.put("SignMsg", subaccounts_select.getSignMsg());

            String query = MapToStringSplicing.createLinkStringByGet(select_map);

            try {
                String result =  HttpClientUtil.getProcessDefinitionList("http://120.55.162.84:8050/api/subaccounts?"+query,select_map);
                JsonParser parser = new JsonParser();
                JsonObject object = (JsonObject) parser.parse(result);
                JsonArray array = null;
                JsonObject subObject = null;
                status = object.get("status").getAsInt();
                if (status == 0) {
                    array = object.get("data").getAsJsonArray();
                    for (int i = 0; i < array.size(); i++) {
                        subObject = array.get(i).getAsJsonObject();
                        SubAccountID = subObject.get("SubAccountID").getAsString();
                        SubAccountName = subObject.get("SubAccountName").getAsString();
                        AgentName = subObject.get("AgentName").getAsString();
                        SubLink = subObject.get("SubLink").getAsString();
                        Link = subObject.get("Link").getAsString();
                    }
                } else {
                    message = object.get("message").getAsString();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            User user = userService.getMemberByMobile(mobile);

            if (user != null && MD5Util.MD5(mobile, password).equals(user.getPassword())) {
                //逸富登录
                Map<String, String> login_map = new LinkedHashMap<String, String>();
                Subaccounts_login login = new Subaccounts_login();
                login.setPassword(password);
                login.setSubAccountID(SubAccountID);
                login.setTimeStamp(time);
                login.setVersion("1.0");
                String str = MD5Util.MD5_login(login.getPassword(), login.getSubAccountID(), login.getTimeStamp(), login.getVersion());
                login.setSignMsg(str);

                login_map.put("Password", login.getPassword());
                login_map.put("SubAccountID", login.getSubAccountID());
                login_map.put("TimeStamp", login.getTimeStamp());
                login_map.put("Version", login.getVersion());
                login_map.put("SignMsg", login.getSignMsg());

                String query1 = MapToStringSplicing.createLinkStringByGet(login_map);

                String result = null;
                try {
                    result =  HttpClientUtil.postHistoryProcessInstancesList("http://120.55.162.84:8050/api/subaccounts/login?"+query1,login_map);
                    JsonParser parser = new JsonParser();
                    JsonObject object = (JsonObject) parser.parse(result);
                    status = object.get("status").getAsInt();
                    message = object.get("message").getAsString();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if(status == 0) {
                    user.setPassword(null);
                    request.getSession().setAttribute(ConstantsUtils.LOGIN_KEY_FLAG, JSON.toJSONString(user));
                    data.setCode(4002);
                    data.setMsg("登录成功");
                    return data;
                }else{
                    data.setCode(status);
                    data.setMsg(message);
                    return data;
                }
            } else {
                data.setCode(4001);
                data.setMsg(" 用户名或密码错误");
                return data;
            }
        } else {
            data.setCode(200);
            return data;
        }
    }


    @RequestMapping("/loginOut")
    public String loginOut(Model model) {
        request.getSession().removeAttribute(ConstantsUtils.LOGIN_KEY_FLAG);
        return "redirect:user/index";
    }

}
